package com.fhnew.app.ui.activity.userinfo.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fhnew.app.R;
import com.fhnew.app.global.Constants;
import com.fhnew.app.model.CustomObserver;
import com.fhnew.app.model.internet.model.UserModel;
import com.fhnew.app.model.network.HttpJsonUtils;
import com.fhnew.app.model.network.entity.req.MsgBoxBean;

import com.fhnew.app.util.OnEndLessScrollListener;
import com.fhnew.app.util.OnItemClickListener;
import com.fhnew.app.util.SYSOUtils;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Administrator on 2019/9/12.
 */

public class StationletterFragment extends Fragment {

    private TextView more;
    private RecyclerView msglist;
    private SwipeRefreshLayout swiprefresh;
    private Adapter adapter;
    private int page;//初始页数

    private OnEndLessScrollListener onEndLessScrollListener;
    private MsgBoxBean mdatas;
    private boolean isrefresh = false;

    private void initView (View view) {
        more = view.findViewById(R.id.more);
        msglist = view.findViewById(R.id.msglist);
        swiprefresh = view.findViewById(R.id.swiprefresh);
    }

    @Nullable
    @Override
    public View onCreateView (@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        container = (ViewGroup) inflater.inflate(R.layout.stationletterlayout,null);
        initView(container);
        return container;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        try {
            page = 0;
            swiprefresh.setColorSchemeResources(android.R.color.holo_blue_bright,
                    android.R.color.holo_green_light, android.R.color.holo_orange_light);
            //给swipeRefreshLayout绑定刷新监听
            swiprefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {
                    //开始访问网络数据
                    isrefresh = true;
                    page = 0;
                    initDatas(page);
                }
            });
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
            onEndLessScrollListener = new OnEndLessScrollListener(linearLayoutManager) {
                @Override
                public void getMoreData(int currentPage) {
                    SYSOUtils.sOut_("触发+++++++++++++++++++++++++");
                    //下拉加载的时候页数加1
                    initDatas(++page);
                }
            };
            msglist.setLayoutManager(linearLayoutManager);
            adapter = new Adapter();
            msglist.setAdapter(adapter);
            //添加滑动监听
            msglist.addOnScrollListener(onEndLessScrollListener);

            initDatas(page);

        } catch (Exception e) {
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
//        try {
//            page = 0;
//            initDatas(page);
//            swiprefresh.setColorSchemeResources(android.R.color.holo_blue_bright,
//                    android.R.color.holo_green_light, android.R.color.holo_orange_light);
//            //给swipeRefreshLayout绑定刷新监听
//            swiprefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
//                @Override
//                public void onRefresh() {
//                    //开始访问网络数据
//                    isrefresh = true;
//                    initDatas(page);
//                }
//            });
//            msglist.setLayoutManager(new LinearLayoutManager(getContext()));
//            adapter = new Adapter();
//        } catch (Exception e) {
//        }
    }

    class Adapter extends RecyclerView.Adapter<Adapter.MViewHolder>
            implements OnItemClickListener {
        @Override
        public Adapter.MViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new Adapter.MViewHolder(LayoutInflater.from(getContext())
                    .inflate(R.layout.msgitem, parent, false));

        }

        @Override
        public void onBindViewHolder(final Adapter.MViewHolder holder, final int position) {
            if (position % 2 != 0) {
                holder.item.setBackgroundResource(R.color.blue);
            } else {
                holder.item.setBackgroundResource(R.color.royalblue);
            }
            holder.text1.setText(mdatas.getData().get(position).getContent());
            holder.text2.setText(mdatas.getData().get(position).getAddDate());
        }

        @Override
        public int getItemCount() {
            return mdatas.getData().size();
        }

        @Override
        public void onItemClick(View view, int position) {

        }

        class MViewHolder extends RecyclerView.ViewHolder {
            TextView text1;
            TextView text2;
            LinearLayout item;

            MViewHolder(View itemView) {
                super(itemView);
                text1 = (TextView) itemView.findViewById(R.id.text1);
                text2 = itemView.findViewById(R.id.text2);
                item = itemView.findViewById(R.id.item);
            }
        }
    }

    private void initDatas(int page) {
        HttpJsonUtils.getHttpApiInstance()
                .get_user_messagebox(Constants.getuseAdress() + "api/m/getUserMessageBox.json",
                        page, 500, UserModel.Token)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(new CustomObserver<>(StationletterFragment.this) {
                    .subscribe(new CustomObserver<MsgBoxBean>() {
                    @Override
                    protected void onCustomNext(MsgBoxBean resBanner) {
                        try {
                            if (isrefresh) {
                                isrefresh = false;
                                swiprefresh.setRefreshing(false);
                            } else {

                            }
                            mdatas = resBanner;
                            if (mdatas == null) {
                                more.setVisibility(View.VISIBLE);
                                swiprefresh.setVisibility(View.GONE);
                            } else {
                                if (mdatas.getData().size() != 0) {
                                    more.setVisibility(View.GONE);
                                    swiprefresh.setVisibility(View.VISIBLE);
                                } else {
                                    more.setVisibility(View.VISIBLE);
                                    swiprefresh.setVisibility(View.GONE);
                                }
                            }
                            adapter.notifyDataSetChanged();
                        } catch (Exception e) {

                        }
                    }

                    @Override
                    protected void onSubscribeCustom(Disposable disposable) {

                    }

                    @Override
                    protected void onErrorCustom(Throwable e) {
                        SYSOUtils.sOut(e.toString());
                    }
                });
    }
}
